home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 2 of 3.iso / chapter5 / setovl.c < prev    next >
C/C++ Source or Header  |  1996-03-06  |  8KB  |  244 lines

  1.  
  2. #define OEMRESOURCE
  3.  
  4. #include <windows.h>  
  5. #include "setovl.h"  
  6. #include "commctrl.h"
  7.  
  8.  
  9. #if defined (WIN32)
  10.     #define IS_WIN32 TRUE
  11. #else
  12.     #define IS_WIN32 FALSE
  13. #endif
  14.  
  15. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  16. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  17. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  18.  
  19. HINSTANCE hInst;   // current instance
  20.  
  21. LPCTSTR lpszAppName = "MyApp";
  22. LPCTSTR lpszTitle   = "ImageList_SetOverlayImage()"; 
  23.  
  24.  
  25. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  26.  
  27.  
  28. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  29.                       LPTSTR lpCmdLine, int nCmdShow)
  30. {
  31.    MSG      msg;
  32.    HWND     hWnd; 
  33.    WNDCLASS wc;
  34.  
  35.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  36.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  37.    wc.cbClsExtra    = 0;                      
  38.    wc.cbWndExtra    = 0;                      
  39.    wc.hInstance     = hInstance;              
  40.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  41.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  42.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  43.    wc.lpszMenuName  = lpszAppName;              
  44.    wc.lpszClassName = lpszAppName;              
  45.  
  46.    if ( IS_WIN95 )
  47.    {
  48.       if ( !RegisterWin95( &wc ) )
  49.          return( FALSE );
  50.    }
  51.    else if ( !RegisterClass( &wc ) )
  52.       return( FALSE );
  53.  
  54.    hInst = hInstance; 
  55.  
  56.    hWnd = CreateWindow( lpszAppName, 
  57.                         lpszTitle,    
  58.                         WS_OVERLAPPEDWINDOW, 
  59.                         CW_USEDEFAULT, 0, 
  60.                         CW_USEDEFAULT, 0,  
  61.                         NULL,              
  62.                         NULL,              
  63.                         hInstance,         
  64.                         NULL               
  65.                       );
  66.  
  67.    if ( !hWnd ) 
  68.       return( FALSE );
  69.  
  70.    ShowWindow( hWnd, nCmdShow ); 
  71.    UpdateWindow( hWnd );         
  72.  
  73.    while( GetMessage( &msg, NULL, 0, 0) )   
  74.    {
  75.       TranslateMessage( &msg ); 
  76.       DispatchMessage( &msg );  
  77.    }
  78.  
  79.    return( msg.wParam ); 
  80. }
  81.  
  82.  
  83. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  84. {
  85.    WNDCLASSEX wcex;
  86.  
  87.    wcex.style         = lpwc->style;
  88.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  89.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  90.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  91.    wcex.hInstance     = lpwc->hInstance;
  92.    wcex.hIcon         = lpwc->hIcon;
  93.    wcex.hCursor       = lpwc->hCursor;
  94.    wcex.hbrBackground = lpwc->hbrBackground;
  95.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  96.    wcex.lpszClassName = lpwc->lpszClassName;
  97.  
  98.    // Added elements for Windows 95.
  99.    //...............................
  100.    wcex.cbSize = sizeof(WNDCLASSEX);
  101.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  102.                             IMAGE_ICON, 16, 16,
  103.                             LR_DEFAULTCOLOR );
  104.             
  105.    return RegisterClassEx( &wcex );
  106. }
  107.  
  108.  
  109. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  110. {
  111. static HIMAGELIST hList  = NULL;
  112. static int        nOvr   = 1;
  113.  
  114.    switch( uMsg )
  115.    {
  116.       case WM_CREATE :
  117.               InitCommonControls();
  118.  
  119.               // Create an image list from an Image.
  120.               //....................................
  121.               hList = ImageList_Create( 32, 32, ILC_MASK, 3, 1 );
  122.               if ( hList )
  123.               {
  124.                  HBITMAP hBmp, hMask;
  125.                  int     nImage;
  126.  
  127.                  // Add the base image.
  128.                  //....................
  129.                  hBmp  = LoadBitmap( hInst, "BMP1" );
  130.                  hMask = LoadBitmap( hInst, "BMP1MSK" );
  131.                  ImageList_Add( hList, hBmp, hMask );
  132.                  DeleteObject( hBmp );
  133.                  DeleteObject( hMask );
  134.  
  135.                  // Add the overlay images.
  136.                  //........................
  137.                  nImage = ImageList_AddIcon( hList, LoadCursor( NULL, IDC_ARROW ) );
  138.                  if ( nImage > -1 )
  139.                     ImageList_SetOverlayImage( hList, nImage, 1 );
  140.  
  141.                  nImage = ImageList_AddIcon( hList, LoadCursor( NULL, IDC_WAIT ) ); 
  142.                  if ( nImage > -1 )
  143.                     ImageList_SetOverlayImage( hList, nImage, 2 );
  144.  
  145.               }
  146.               break;
  147.  
  148.       case WM_PAINT :
  149.               {
  150.                  PAINTSTRUCT ps;
  151.  
  152.                  BeginPaint( hWnd, &ps );
  153.  
  154.                  // Draw the image with the overlay image.
  155.                  //.......................................
  156.                  ImageList_Draw( hList, 0, ps.hdc, 10, 10, INDEXTOOVERLAYMASK( nOvr ) );
  157.  
  158.                  EndPaint( hWnd, &ps );
  159.               }
  160.               break;
  161.  
  162.       case WM_COMMAND :
  163.               switch( LOWORD( wParam ) )
  164.               {
  165.                         // Change the base image.
  166.                         //.......................
  167.                  case IDM_IMAGE1 :
  168.                  case IDM_IMAGE2 :
  169.                         {
  170.                            HBITMAP hBmp, hMask;
  171.  
  172.                            hBmp  = LoadBitmap( hInst, LOWORD( wParam ) == IDM_IMAGE1 ? 
  173.                                                "BMP1" : "BMP2" );
  174.                            hMask = LoadBitmap( hInst, LOWORD( wParam ) == IDM_IMAGE1 ? 
  175.                                                "BMP1MSK" : "BMP2MSK" );
  176.                            ImageList_Replace( hList, 0, hBmp, hMask );
  177.                            DeleteObject( hBmp );
  178.                            DeleteObject( hMask );
  179.                            InvalidateRect( hWnd, NULL, TRUE );
  180.                         }
  181.                         break;
  182.  
  183.                         // Set the overlay image to the arrow.
  184.                         //....................................
  185.                  case IDM_ARROW :
  186.                         nOvr = 1;
  187.                         InvalidateRect( hWnd, NULL, TRUE );
  188.                         break;
  189.  
  190.                         // Set the overlay image to the hourglass.
  191.                         //........................................
  192.                  case IDM_WAITING :
  193.                         nOvr = 2;
  194.                         InvalidateRect( hWnd, NULL, TRUE );
  195.                         break;
  196.  
  197.                  case IDM_ABOUT :
  198.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  199.                         break;
  200.  
  201.                  case IDM_EXIT :
  202.                         DestroyWindow( hWnd );
  203.                         break;
  204.               }
  205.               break;
  206.       
  207.       case WM_DESTROY :
  208.               if ( hList )
  209.                  ImageList_Destroy( hList );
  210.  
  211.               PostQuitMessage(0);
  212.               break;
  213.  
  214.       default :
  215.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  216.    }
  217.  
  218.    return( 0L );
  219. }
  220.  
  221.  
  222. LRESULT CALLBACK About( HWND hDlg,           
  223.                         UINT message,        
  224.                         WPARAM wParam,       
  225.                         LPARAM lParam)
  226. {
  227.    switch (message) 
  228.    {
  229.        case WM_INITDIALOG: 
  230.                return (TRUE);
  231.  
  232.        case WM_COMMAND:                              
  233.                if (   LOWORD(wParam) == IDOK         
  234.                    || LOWORD(wParam) == IDCANCEL)    
  235.                {
  236.                        EndDialog(hDlg, TRUE);        
  237.                        return (TRUE);
  238.                }
  239.                break;
  240.    }
  241.  
  242.    return (FALSE); 
  243. }
  244.